

#include <linkedlist.h>
#include <types.h>

/**
 * Sort a linked list given a sorter function
 * 
 * linkedlist_t *list - list to sort
 * f_sort sorter - sorting function
 * 
 * returns - sorted list
 */
linkedlist_t * linkedlist_sort(linkedlist_t *list, f_sort sorter)
{
	return NULL;
}

/**
 * Insert a node after a given node in a linked list (doubly linked)
 * 
 * linkedlist_t *node - node to insert
 * linkedlist_t *ref - node to insert after
 */
void linkedlist_insert_after(linkedlist_t *node, linkedlist_t *ref)
{
	if (ref != NULL)
	{
		if (ref->next != NULL)
			ref->next->prev = node;
		node->next = ref->next;
		ref->next = node;
	}
	
	node->prev = ref;
}

/**
 * Insert a node before a given node in a linked list (doubly linked)
 * 
 * linkedlist_t *node - node to insert
 * linkedlist_t *ref - node to insert before
 */
void linkedlist_insert_before(linkedlist_t *node, linkedlist_t *ref)
{
	if (ref != NULL)
	{
		if (ref->prev != NULL)
			ref->prev->next = node;
		node->prev = ref->prev;
		ref->prev = node;
	}
	
	node->next = ref;
}

/**
 * Insert a node after a given node in a linked list (singly linked)
 * 
 * slinkedlist_t *node - node to insert
 * slinkedlist_t *ref - node to insert after
 */
void slinkedlist_insert_after(slinkedlist_t *node, slinkedlist_t *ref)
{
	if (ref != NULL)
	{
		node->next = ref->next;
		ref->next = node;
	}
}

/**
 * Insert a node before a given node in a linked list (singly linked)
 * 
 * slinkedlist_t *node - node to insert
 * slinkedlist_t *ref - node to insert before
 */
void slinkedlist_insert_before(slinkedlist_t *node, slinkedlist_t *ref)
{
	node->next = ref;
}
